<?php

namespace app\api\controller;

use app\facade\Redis;
use think\Validate;
use app\facade\Mns;
use app\models\{Region, PlatClass, Banner, Goods, Business, GoodsSpec, Wechat, Config, Pgoods, PgoodsSpec, Porder, PorderGoods, Address, User as Users, Order, Annou};
use think\facade\Env;
use think\Db;
use oss\Oss;
use think\db\Where;
use EasyWeChat\Factory;


class Other extends Base
{
    public function others()
    {
        $name = $this->request->get('name', '');
        $result = (new Config())->where('name', $name)->value('value') ?? '';
        if ($name == 'mybank') {
            $result = json_decode($result, true) ?? [];
        }
        $this->result($result, 0, '获取成功');
    }

    public function annous()
    {
        $type = $this->request->get('type/d', 0);
        if (empty($type)) {
            $result = (new Annou())->where('isshow', 1)->order('sort desc')->select();
        } else {
            $result = (new Annou())->where('isshow', 1)->order('sort desc')->find();
        }
        $this->result($result, 0, '获取成功');
    }

    public function oss()
    {
        if ($this->request->isPost()) {
            $file = request()->file('file');
            if (empty($_FILES['file'])) {
                $this->result([], 1, '参数错误');
            }
            $path =  $_SERVER['DOCUMENT_ROOT'] . "/uploads/";
            $info = $file->validate(['size' => 1024 * 1024 * 5, 'ext' => 'jpg,png,gif,jpeg'])
                ->move($path);
            if (!$info) {
                $this->result([], 1, $file->getError());
            } else {
                $url = '/uploads' . '/' . date("Ymd") . '/' . $info->getfileName();
                $url = url($url, '', '', false);
                $url = "https://" . Env('app.api_url') . $url;
                $this->result($url, 0, '获取成功');
            }
        } else {
            $this->result([], 1, '请求错误');
        }
    }

    public function addressjson()
    {
        $addressjsonredis = 'addressjsonredis';
        $haveaddress = Redis::get($addressjsonredis);
        if ($haveaddress) {
            $result = unserialize($haveaddress);
        } else {
            $region = (new Region())->field('name as label,area_code as value,id,parent_id,level')->where(['level' => [1, 2, 3]])->select();
            $resultson = [];
            $results = [];
            $result = [];
            foreach ($region as $regions) {
                if ($regions['level'] == 3) {
                    $resultson[$regions['parent_id']][] = [
                        'label' => $regions['label'],
                        'value' => $regions['value']
                    ];
                }
            }
            foreach ($region as $regions) {
                if ($regions['level'] == 2) {
                    $results[$regions['parent_id']][] = [
                        'label' => $regions['label'],
                        'value' => $regions['value'],
                        'children' => $resultson[$regions['id']] ?? []
                    ];
                }
            }
            foreach ($region as $regions) {
                if ($regions['level'] == 1) {
                    $result[] = [
                        'label' => $regions['label'],
                        'value' => $regions['value'],
                        'children' => $results[$regions['id']] ?? []
                    ];
                }
            }
            Redis::setex($addressjsonredis, 60 * 60 * 3, serialize($result));
        }
        return json($result);
    }

    public function banner()
    {
        $result = (new Banner())->field('title,id,url')->select();
        $this->result($result, 0, '获取成功');
    }
}
